package com.stgy.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stgy.lease.common.exception.StgyException;
import com.stgy.lease.common.result.ResultCodeEnum;
import com.stgy.lease.model.domain.AttrValue;
import com.stgy.lease.model.domain.FacilityInfo;
import com.stgy.lease.web.admin.service.AttrValueService;
import com.stgy.lease.web.admin.mapper.AttrValueMapper;
import org.springframework.stereotype.Service;

/**
 * @author lingx
 * @description 针对表【attr_value(房间基本属性值表)】的数据库操作Service实现
 * @createDate 2024-08-28 11:00:28
 */
@Service
public class AttrValueServiceImpl extends ServiceImpl<AttrValueMapper, AttrValue>
        implements AttrValueService {

    @Override
    public boolean saveOrUpdateAttrValue(AttrValue attrValue) {
        if (attrValue == null || attrValue.getName() == null) {
            throw new StgyException(ResultCodeEnum.DATA_EXISTS_ERROR);
        }
        LambdaQueryWrapper<AttrValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrValue::getName, attrValue.getName());
        wrapper.eq(AttrValue::getAttrKeyId, attrValue.getAttrKeyId());
        wrapper.last("limit 1");
        AttrValue one = this.getOne(wrapper);
        //唯一校验 type+name 组合唯一
        if (one != null && !one.getId().equals(attrValue.getId())) {
//        if (one != null && one.getId().longValue() != facilityInfo.getId()) {
            throw new StgyException(ResultCodeEnum.DATA_EXISTS_ERROR);
        }

        return this.saveOrUpdate(attrValue);
    }
}




